//
//  SNCompilePlanViewController.m
//  ZhuoxinAgriculture
//
//  Created by stone on 16/12/25.
//  Copyright © 2016年 stone. All rights reserved.
//

#import "SNCheckPesticideCell.h"
#import "SNCompilePlanMoreCell.h"
#import "SNCheckPlanFertilizerCell.h"
#import "SNCompilePlanSummaryCell.h"
#import "SNCompilePlanSeedCell.h"

#import "SNCheckPlanSectionHeaderView.h"
#import "SNCompilePlanOtherCell.h"
#import "SNCheckPlanViewController.h"
#import "SNCompilePlanTopView.h"
#import "Masonry.h"
#import "SNCompilePlanCell.h"
#import "SNPlanListViewController.h"

@interface SNCheckPlanViewController ()<UITableViewDataSource,UITableViewDelegate>
/** normalView */
@property (weak, nonatomic) UIView * bottomView;
/** mmButton */
@property(weak, nonatomic) UIButton * saveButton;
/** tableView */
@property (nonatomic,weak) UITableView * tableView;
/** View */
@property (weak, nonatomic)  SNCompilePlanTopView *topView;
@end

@implementation SNCheckPlanViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.rowCount = 1;
    self.navigationItem.title = @"查看生产计划";
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = kNormalBackgroundColor;
    // Do any additional setup after loading the view, typically from a nib.
    [self addTopView];
    
    [self.view layoutIfNeeded];
    [self addTableView];
}
- (void)addTableView
{
    /** y坐标 */
    CGFloat y = CGRectGetMaxY(self.topView.frame)+17;
    /** 高度 tableView */
    CGFloat height = self.view.bounds.size.height - y;
    /** 样式 tableView */
    UITableViewStyle tableViewStyle = UITableViewStyleGrouped;
    
    CGRect frame = CGRectMake(0, y, self.view.bounds.size.width, height);
    
    UITableView * tableView = [[UITableView alloc] initWithFrame:frame style:tableViewStyle];
    self.tableView = tableView;
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.backgroundColor = kNormalBackgroundColor;
    [self setupInit:tableView];
    
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    //    tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
    //    tableView.separatorColor = [UIColor greenColor];
    [self.view addSubview:tableView];
    
    [self addObserver];
    [self addRequest];
}
- (void)addTopView
{
    SNCompilePlanTopView *topView = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([SNCompilePlanTopView class]) owner:nil options:nil] lastObject];
    self.topView = topView;
    topView.model = self.model;
    topView.plantingDecisionMakerResult = self.plantingDecisionMakerResult;
    [self.view addSubview:topView];
    
    [topView mas_makeConstraints:^(MASConstraintMaker * make) {
        make.left.mas_equalTo(self.view.mas_left).offset(12);
        make.right.mas_equalTo(self.view.mas_right).offset(-12);
        make.top.mas_equalTo(self.view.mas_top).offset(64);
    }];
}

//sn_note:========= tableView ============================ stone 🐳 ===========/
#pragma mark - <addRequest>
- (void)addRequest {
    
}
#pragma mark - <addObserver>
- (void)addObserver {
    // addObserver
}
- (void)dealloc {
    NSLog(@"%@ %s %@",[self class], __func__ , @"im dying");
}
/** 初始化 tableView */
- (void)setupInit:(UITableView *)tableView
{
    //MARK: - 设置tableHeaderView
    //    {
    //        UIView * tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 17)];
    //        tableHeaderView.backgroundColor = kNormalBackgroundColor;
    //        tableView.tableHeaderView = tableHeaderView;
    //    }
    
    //MARK: - 注册
    {
        /** xib 注册cell */
        registerForCellFromeNib(tableView,SNCompilePlanCell);
        registerForCellFromeNib(tableView,SNCompilePlanOtherCell);
        registerForCellFromeNib(tableView,SNCompilePlanSeedCell);
        registerForCellFromeNib(tableView,SNCompilePlanSummaryCell);
        registerForCellFromeNib(tableView,SNCheckPlanFertilizerCell);
        registerForCellFromeNib(tableView,SNCompilePlanMoreCell);
        registerForCellFromeNib(tableView,SNCheckPesticideCell);
        
        /** class 注册cell */
        //  registerForCellFromeClass(tableView,UITableViewCell);
        
        /** xib 注册 headerSection */
        registerForHeaderFooterViewFromeNib(tableView,SNCheckPlanSectionHeaderView);
        
        
        /** class 注册 headerSection */
        //    registerForHeaderFooterViewFromeClass(tableView,UITableViewHeaderFooterView);
        
        /** xib 注册 footerSection */
        //    registerForHeaderFooterViewFromeNib(tableView,UITableViewHeaderFooterView);
        /** class 注册 footerSection */
        //    registerForHeaderFooterViewFromeClass(tableView,UITableViewHeaderFooterView);
    }
    //MARK: - 设置高度
    {
        /** header */
        //        tableView.sectionHeaderHeight = 17;
        tableView.estimatedSectionHeaderHeight = 100;
        tableView.sectionHeaderHeight = UITableViewAutomaticDimension;
        /** cell */
        tableView.estimatedRowHeight = 100;
        tableView.rowHeight = UITableViewAutomaticDimension;
        /** footer */
        tableView.sectionFooterHeight = 17;
        // tableView.estimatedSectionFooterHeight = 100;
        // tableView.sectionFooterHeight = UITableViewAutomaticDimension;
    }
}

#pragma mark - <UITableViewDataSource>
/** 组数 */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 4;
}
/** 行数 */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    switch (section) {
        case 0:
            return 1;
            break;
        case 1:
            return 1;
            break;
        case 2:
            return self.productionPlanListResult.fertList.count;
            break;
        case 3:
            return self.productionPlanListResult.pestList.count;
            break;
        default:
            break;
    }
    
    return self.rowCount;
}
//MARK: -
//MARK: - cell
/** cell */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell * cell = nil;
    switch (indexPath.section) {
        case 0:
        {
            SNCompilePlanSummaryCell * cellTemp = DequeueForCell(SNCompilePlanSummaryCell);
            [cellTemp configCellWithTitle:@"概要信息" imageName:@"05-earth"];
            
            cellTemp.productionPlanListResult = self.productionPlanListResult;
            cellTemp.nav = self.navigationController;
            cell = cellTemp;
        }
            break;
        case 1:
        {
            SNCompilePlanSeedCell * cellTemp = (SNCompilePlanSeedCell *)DequeueForCell(SNCompilePlanSeedCell);
            [cellTemp configCellWithTitle:@"种子信息" imageName:@"05-seed"];
            cellTemp.productionPlanListResult = self.productionPlanListResult;
            
            cellTemp.nav = self.navigationController;
            cell = cellTemp;
        }
            break;
        case 2:
        {
            SNCheckPlanFertilizerCell * cellTemp = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([SNCheckPlanFertilizerCell class])];
            NSString *title = [NSString stringWithFormat:@"肥料信息( %zd )",indexPath.row+1];
            [cellTemp configCellWithTitle:title imageName:@"11-flow" indexPath:indexPath];
            
            cellTemp.productionPlanListResult = self.productionPlanListResult;
            
            
            
            cellTemp.nav = self.navigationController;
            
            cell = cellTemp;
        }
            break;
        case 3:
        {
            SNCheckPesticideCell * cellTemp = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([SNCheckPesticideCell class])];
            NSString *title = [NSString stringWithFormat:@"农药信息( %zd )",indexPath.row+1];
            [cellTemp configCellWithTitle:title imageName:@"icon_pesticide" indexPath:indexPath];
            cellTemp.productionPlanListResult = self.productionPlanListResult;
            
            cellTemp.nav = self.navigationController;
            cell = cellTemp;
        }
            break;
            
        default:
            break;
    }
    
    
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    return cell;
}
#pragma mark - <UITableViewDelegate>
/** 自定义section header */
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UITableViewHeaderFooterView * headerView = nil;
    
    switch (section) {
        case 0:
        {
            SNCheckPlanSectionHeaderView * headerViewTemp = DequeueForHeaderFooterView(SNCheckPlanSectionHeaderView);
            [headerViewTemp configCellWithTitle:@"概要信息" imageName:@"05-earth"];
            headerViewTemp.nav = self.navigationController;
            headerView = headerViewTemp;
        }
            break;
        case 1:
        {
            SNCheckPlanSectionHeaderView * headerViewTemp = DequeueForHeaderFooterView(SNCheckPlanSectionHeaderView);
            [headerViewTemp configCellWithTitle:@"种子信息" imageName:@"05-seed"];
            headerViewTemp.nav = self.navigationController;
            headerView = headerViewTemp;        }
            break;
        case 2:
        {
            SNCheckPlanSectionHeaderView * headerViewTemp = DequeueForHeaderFooterView(SNCheckPlanSectionHeaderView);
            [headerViewTemp configCellWithTitle:@"肥料信息" imageName:@"05-fertilizer"];
            headerViewTemp.nav = self.navigationController;
            headerView = headerViewTemp;
        }
            break;
        case 3:
        {
            SNCheckPlanSectionHeaderView * headerViewTemp = DequeueForHeaderFooterView(SNCheckPlanSectionHeaderView);
            [headerViewTemp configCellWithTitle:@"农药信息" imageName:@"05-medicine"];
            headerViewTemp.nav = self.navigationController;
            headerView = headerViewTemp;
        }
            break;
            
        default:
            break;
    }
    
    return headerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return -1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return 17;
}
/** 选中一行 */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"%s", __func__);
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
/** 取消 选中 */
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0) {
    NSLog(@"%s", __func__);
}
/** 可编辑 */
//- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    return YES;
//}
/** 删除样式 */
//- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    return UITableViewCellEditingStyleDelete;
//}
/** 删除当前行 */
//- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
//
//    if (editingStyle == UITableViewCellEditingStyleDelete) {
//
//        [self.models removeObjectAtIndex:indexPath.row];
//
//        [tableView deleteRowsAtIndexPaths:@[ indexPath ] withRowAnimation:UITableViewRowAnimationLeft];
//}

@end
